package com.gk.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.study.entity.Room;
import com.gk.study.entity.RoomTag;
import com.gk.study.mapper.RoomMapper;
import com.gk.study.mapper.RoomTagMapper;
import com.gk.study.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    RoomMapper mapper;

    @Autowired
    RoomTagMapper roomTagMapper;

    @Override
    public List<Room> getRoomList(String keyword, String sort, String c, String tag) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();

        // 搜索
        queryWrapper.like(StringUtils.isNotBlank(keyword), "title", keyword);

        // 排序
        if (StringUtils.isNotBlank(sort)) {
            if (sort.equals("recent")) {
                queryWrapper.orderBy(true, false, "create_time");
            } else if (sort.equals("hot") || sort.equals("recommend")) {
                queryWrapper.orderBy(true, false, "pv");
            }
        }else {
            queryWrapper.orderBy(true, false, "create_time");
        }

        // 根据分类筛选
        if (StringUtils.isNotBlank(c) && !c.equals("-1")) {
            queryWrapper.eq(true, "classification_id", c);
        }

        List<Room> rooms = mapper.selectList(queryWrapper);

        // tag筛选
        if (StringUtils.isNotBlank(tag)) {
            List<Room> tRooms = new ArrayList<>();
            QueryWrapper<RoomTag> thingTagQueryWrapper = new QueryWrapper<>();
            thingTagQueryWrapper.eq("tag_id", tag);
            List<RoomTag> roomTagList = roomTagMapper.selectList(thingTagQueryWrapper);
            for (Room room : rooms) {
                for (RoomTag roomTag : roomTagList) {
                    if (room.getId().equals(roomTag.getRoomId())) {
                        tRooms.add(room);
                    }
                }
            }
            rooms.clear();
            rooms.addAll(tRooms);
        }

        // 附加tag
        for (Room room : rooms) {
            QueryWrapper<RoomTag> thingTagQueryWrapper = new QueryWrapper<>();
            thingTagQueryWrapper.lambda().eq(RoomTag::getRoomId, room.getId());
            List<RoomTag> roomTags = roomTagMapper.selectList(thingTagQueryWrapper);
            List<Long> tags = roomTags.stream().map(RoomTag::getTagId).collect(Collectors.toList());
            room.setTags(tags);
        }
        return rooms;
    }

    @Override
    public void createRoom(Room room) {
        System.out.println(room);
        room.setCreateTime(String.valueOf(System.currentTimeMillis()));

        if (room.getPv() == null) {
            room.setPv("0");
        }
        if (room.getScore() == null) {
            room.setScore("0");
        }
        if (room.getWishCount() == null) {
            room.setWishCount("0");
        }
        mapper.insert(room);
        // 更新tag
        setRoomTags(room);
    }

    @Override
    public void deleteRoom(String id) {
        mapper.deleteById(id);
    }

    @Override
    public void updateRoom(Room room) {

        // 更新tag
        setRoomTags(room);

        mapper.updateById(room);
    }

    @Override
    public Room getRoomById(String id) {
        return mapper.selectById(id);
    }

    // 心愿数加1
    @Override
    public void addWishCount(String roomId) {
        Room room = mapper.selectById(roomId);
        room.setWishCount(String.valueOf(Integer.parseInt(room.getWishCount()) + 1));
        mapper.updateById(room);
    }

    // 收藏数加1
    @Override
    public void addCollectCount(String roomId) {
        Room room = mapper.selectById(roomId);
        room.setCollectCount(String.valueOf(Integer.parseInt(room.getCollectCount()) + 1));
        mapper.updateById(room);
    }

    public void setRoomTags(Room room) {
        // 删除tag
        Map<String, Object> map = new HashMap<>();
        map.put("room_id", room.getId());
        roomTagMapper.deleteByMap(map);
        // 新增tag
        if (room.getTags() != null) {
            for (Long tag : room.getTags()) {
                RoomTag roomTag = new RoomTag();
                roomTag.setRoomId(room.getId());
                roomTag.setTagId(tag);
                roomTagMapper.insert(roomTag);
            }
        }
    }

}
